<?php
global $_GPC, $_W;
$weid = $this->_weid;
$returnid = $this->checkPermission();
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$setting = $this->getSetting();
$stores = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " WHERE weid = :weid AND deleted=0 ORDER BY id DESC", array(':weid' => $this->_weid), 'id');
$levellist = pdo_fetchall("SELECT * FROM " . tablename("weisrc_dish_marketinglevel") . " WHERE weid = :weid  ORDER BY id DESC", array(':weid' => $this->_weid),'id');


$GLOBALS['frames'] = $this->getMainMenu();
if ($operation == 'post') {

    $id = intval($_GPC['id']);

    if (!empty($id)) {
        $marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND id=:id ORDER BY id DESC", array(':weid' => $this->_weid, ':id' => $id));
        $fans = $this->getFansByOpenid($marketing['from_user']);
    }

    if (checksubmit('submit')) {
        if (empty($id)) {
            $data = array(
                'weid' => $this->_weid,
                'from_user' => trim($_GPC['from_user']),
                'username' => trim($_GPC['username']),
                'mobile' => trim($_GPC['mobile']),
                'levelid' => trim($_GPC['levelid']),
                'bankname' => trim($_GPC['bankname']),
                'bankcard' => trim($_GPC['bankcard']),
                'dateline' => TIMESTAMP,
            );
            pdo_insert('weisrc_dish_marketing', $data);
            message('用户增加成功！!', $this->createWebUrl('marketing', array(), true));
        } else {
            $data = array(
                'weid' => $this->_weid,
                'from_user' => trim($_GPC['from_user']),
                'username' => trim($_GPC['username']),
                'mobile' => trim($_GPC['mobile']),
                'levelid' => trim($_GPC['levelid']),
                'bankname' => trim($_GPC['bankname']),
                'bankcard' => trim($_GPC['bankcard']),
            );

            pdo_update('weisrc_dish_marketing', $data, array('id' => $id));
            message('更新成功！', $this->createWebUrl('marketing', array(), true));
        }
        message('操作成功！');
    }
} else if ($operation == 'display') {
    $strwhere = ' WHERE weid =:weid ';
    $pindex = max(1, intval($_GPC['page']));
    $psize = 10;
    $list = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketing') . " {$where} order by storetotal desc,id desc LIMIT " . ($pindex - 1) * $psize . ",{$psize}");

    if (!empty($list)) {
        foreach ($list as $key => $value) {
            $storetotal = pdo_fetchcolumn("SELECT count(1) FROM " . tablename($this->table_stores) . "
WHERE weid =:weid AND marketingid=:marketingid ", array(':weid' => $this->_weid, ':marketingid' => $value['id']));
            $list[$key]['storetotal'] = $storetotal;
        }

        $total = pdo_fetchcolumn("SELECT count(1) FROM " . tablename('weisrc_dish_marketing') . " {$where} order by displayorder desc,id desc", array(':weid' => $this->_weid));
        $pager = pagination($total, $pindex, $psize);
    }
} else if ($operation == 'log') {
    load()->func('tpl');
    $sess_id = $_SESSION['sess_id'] = mt_rand(1000, 9999);


    $strwhere = " weid = '{$_W['uniacid']}' ";
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']) + 86399;
        $strwhere .= " AND dateline >= :starttime AND dateline <= :endtime ";
        $paras[':starttime'] = $starttime;
        $paras[':endtime'] = $endtime;
    }

    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime = time();
    }


    if (isset($_GPC['status']) && $_GPC['status']!='') {
        $strwhere .= " AND status = :status ";
        $paras[':status'] = intval($_GPC['status']);
    }

    $pindex = max(1, intval($_GPC['page']));
    $psize = 10;
    $list = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketinglog') . " WHERE {$strwhere} ORDER BY id DESC LIMIT
" . ($pindex - 1) * $psize . ",{$psize}" , $paras);

    if (!empty($list)) {
        foreach($list as $key => $value) {
            $marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND id=:id LIMIT 1;", array(':weid' => $this->_weid, ':id' => $value['marketingid']));
            $list[$key]['bankname'] = $marketing['bankname'];
            $list[$key]['bankcard'] = $marketing['bankcard'];
        }
        $total = pdo_fetchcolumn("SELECT count(1) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE {$strwhere}
        ", $paras);
        $pager = pagination($total, $pindex, $psize);
    }
}elseif ($operation == 'setstatus') {

    if ($_W['role'] == 'manager' || $_W['isfounder']) {
        $id = intval($_GPC['id']);
        $item = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketinglog') . " WHERE weid =:weid AND id=:id ORDER BY id DESC LIMIT 1", array(':weid' => $weid, ':id' => $id));


        if (!empty($item) && $item['status']==0) {
            $storeid = $item['storeid'];
            $data = array(
                'status' => 1,
                'handletime' => TIMESTAMP,
            );
            pdo_update('weisrc_dish_marketinglog', $data, array('id' => $id, 'weid' => $weid));
            message('提现成功！', $this->createWebUrl('marketing', array('op' => 'log')), 'success');
        }
    }
} elseif ($operation == 'delete') {
    if ($_W['role'] == 'manager' || $_W['isfounder']) {
        $id = intval($_GPC['id']);
        $item = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid =:weid AND id=:id ORDER BY id DESC LIMIT 1", array(':weid' => $weid, ':id' => $id));
        if ($item) {
            $storetotal = pdo_fetchcolumn("SELECT count(1) FROM " . tablename($this->table_stores) . "
WHERE weid =:weid AND marketingid=:marketingid ", array(':weid' => $this->_weid, ':marketingid' => $item['id']));
            if ($storetotal == 0) {
                pdo_delete('weisrc_dish_marketing', array('weid' => $weid, 'id' => $id));
                message('操作成功！', $this->createWebUrl('marketing', array('op' => 'display')), 'success');
            } else {
                message('专员有门店数据不能删除！', $this->createWebUrl('marketing', array('op' => 'display')), 'error');
            }
        }
    } else {
        message('您没有相关的操作权限！');
    }
}
include $this->template('web/marketing');